package com.sjzd.meeting.controller;

import com.sjzd.meeting.pojo.User;
import com.sjzd.meeting.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 用户绑定
     * 可能会发生四种情况：
     * 1.该用户不存在，提示用户
     * 2.该邮箱已经被绑定， 提示用户
     * 3.邮箱已被拉黑，提示用户
     * 4.正常登录，跳转至用户消息页面
     */
    @PostMapping("/login")
    @ResponseBody
    public String login(@RequestParam("email") final String email,
                        @RequestParam("wxid") final Integer wxid, HttpSession session) {
        User user = userService.selectByEmail(email);
        if (null == user) {  // 不存在
            return "1";
        } else {             // 存在
            if (user.getWxid() != null) {
                return "2";
            }
            if (!user.getStatus()) {
                return "3";
            }
            user.setWxid(wxid);
            userService.update(user);
            return "4";
        }
    }

    /**
     *  更新用户信息
     * @return
     */
    @PostMapping("/userInfoUpdate")
    @ResponseBody
    public String update(User user) {
        int flag = userService.update(user);
        return flag == 1 ? "2" : "1";
    }

    @RequestMapping("/candidateInfo")
    public String userInfo(Integer id, Model model) {
        User user = userService.selectById(id);
        return "weixin/candidateInfo";
    }
}
